<%@page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="com.ubmap.tube.*"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<c:set var="geoCodeProcessNo" value="${ geoCodeProcessNo + 1 }" scope="application" />

<jsp:useBean id="tubeWebInput" class="com.ubmap.tube.TubeWebInput" scope="request">
    <jsp:setProperty name="tubeWebInput" property="*" /> 
</jsp:useBean>

<c:if test="${ false }">
    ${tubeWebInput.geoCodeRequest }
</c:if>

<c:if test="${ true }"> 
    <%// request geocode service by HTTP GET method %>
    <%
        boolean localDebug = true ; 
    
        String geoCodeProcessNo = "" + application.getAttribute( "geoCodeProcessNo" );
	    if( localDebug ) { 
	        System.out.println( geoCodeProcessNo + ": Request geocoding service V1.0.135 .... " );
	        System.out.println( "addr: " + tubeWebInput.getQ() );
	        System.out.println( "geoCodeRequest: " + tubeWebInput.getGeoCodeRequest() );
	    } 
    %> 
    <c:catch var ="geoCodeException">
        <c:if test="${ false  }">
			<c:import url="http://api.ubmap.co.kr:8000/geo" charEncoding="UTF-8" >
			    <c:param name="param">${ tubeWebInput.geoCodeRequest.xml }</c:param>
			</c:import> 
		</c:if>
		<c:if test="${ true }">
            <c:import url="http://api.ubmap.co.kr:8000/geo" charEncoding="UTF-8" var="geoCodeServerResponse" scope="request" >
                <c:param name="param">${ tubeWebInput.geoCodeRequest.xml }</c:param>
            </c:import>
        </c:if>
	</c:catch>
	<c:if test="${ geoCodeException != null }">
	   <geoCodeServerError></geoCodeServerError>
	</c:if>
	<%   
	   if( true ) { 
			String geoCodeServerResponse = "" + request.getAttribute( "geoCodeServerResponse" ); 
	        geoCodeServerResponse = geoCodeServerResponse.trim();
			// clear jsp writer buffer 
		    out.clearBuffer();
			//out.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" );
			out.write( geoCodeServerResponse );
		    out.flush();
		    out.close();
			if( localDebug ) { 
				System.out.println( "geoCode Server Response:" ); 
				System.out.println( geoCodeServerResponse ); 
			    System.out.println( geoCodeProcessNo + ": Done service !!!! " ); 
			} 
	   }
    %> 
</c:if>